package com.hmall.service;

import com.alibaba.fastjson.JSON;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.*;
import com.hmall.util.TreeUtil;
import com.hmall.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ShopManageService {
    private final ShopAttrMapper shopAttrMapper;

    private final ShopAttrValueMapper shopAttrValueMapper;

    private final ShopCategoryMapper shopCategoryMapper;

    private final ShopProductMapper shopProductMapper;

    private final ShopProductValueMapper shopProductValueMapper;

    private final MallShopMapper mallShopMapper;

    private final ShopGradeMapper shopGradeMapper;

    private final ShopProductGradeMapper shopProductGradeMapper;

    private final ShopSharesMapper shopSharesMapper;

    private final ShopUserSharesMapper shopUserSharesMapper;


    @Autowired
    public ShopManageService(ShopAttrMapper shopAttrMapper, ShopAttrValueMapper shopAttrValueMapper, ShopCategoryMapper shopCategoryMapper, ShopProductMapper shopProductMapper, ShopProductValueMapper shopProductValueMapper, MallShopMapper mallShopMapper, ShopGradeMapper shopGradeMapper, ShopProductGradeMapper shopProductGradeMapper, ShopSharesMapper shopSharesMapper, ShopUserSharesMapper shopUserSharesMapper) {
        this.shopAttrMapper = shopAttrMapper;
        this.shopAttrValueMapper = shopAttrValueMapper;
        this.shopCategoryMapper = shopCategoryMapper;
        this.shopProductMapper = shopProductMapper;
        this.shopProductValueMapper = shopProductValueMapper;
        this.mallShopMapper = mallShopMapper;
        this.shopGradeMapper = shopGradeMapper;
        this.shopProductGradeMapper = shopProductGradeMapper;
        this.shopSharesMapper = shopSharesMapper;
        this.shopUserSharesMapper = shopUserSharesMapper;
    }


    public ServerResponse bulkUpdateProduct(BulkProductVo bulkProductVo) {
        Integer shop_id = mallShopMapper.selectByDeptId(bulkProductVo.getDept_id());
        if (shop_id == null) {
            return ServerResponse.createByErrorMessage("商家不存在");
        }
        shopProductMapper.updateProductDetail(bulkProductVo.getCategoryId(), bulkProductVo.getDetail(), bulkProductVo.getImages(), shop_id);
        return ServerResponse.createBySuccess("修改成功");
    }

    public ServerResponse updateProduct(ShopProductVo shopProductVo) {
        try {
            ShopProduct shopProduct = new ShopProduct();
            BeanUtils.copyProperties(shopProductVo, shopProduct);
            shopProduct.setCreateTime(new Date());
            shopProductMapper.updateByPrimaryKey(shopProduct);
            Integer product_id = shopProductVo.getProductId();
            //删除旧属性连接
            shopProductValueMapper.updateStatusByProductId(product_id);
            shopProductGradeMapper.updateStatusByProductId(product_id);
            //添加新连接
            ShopProductValue shopProductValue = new ShopProductValue();
            shopProductValue.setProductId(product_id);
            shopProductValue.setCreateTime(new Date());
            shopProductValue.setStatus(1);
            //添加单选框属性
            for (Integer key : shopProductVo.getAttrMap().keySet()) {
                shopProductValue.setAttrValueId(shopProductVo.getAttrMap().get(key));
                shopProductValueMapper.insert(shopProductValue);
            }
            //添加输入框属性
            addInputBoxValue(shopProductVo, shopProductValue);
            //添加等级
            ShopProductGrade shopProductGrade = new ShopProductGrade();
            shopProductGrade.setCreateTime(new Date());
            shopProductGrade.setProductId(product_id);
            shopProductGrade.setStatus(1);
            addGrade(shopProductVo, shopProductGrade);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("参数有异常，请联系后端人员");
        }
        return ServerResponse.createBySuccess("更新成功");
    }


    //以下为复用方法

    private void addGrade(@RequestBody ShopProductVo shopProductVo, ShopProductGrade shopProductGrade) {
        for (String key : shopProductVo.getGradeMap().keySet()) {
            shopProductGrade.setGrade(key);
            String value = shopProductVo.getGradeMap().get(key);
            BigDecimal stock = new BigDecimal(value.split(",")[0]);
            BigDecimal stockLine = new BigDecimal(value.split(",")[1]);
            shopProductGrade.setStock(stock);
            shopProductGrade.setStockWarningLine(stockLine);
            shopProductGradeMapper.insert(shopProductGrade);
        }
    }

    private List<CategoryTree> getCategoryTree(List<ShopCategory> categorys) {
        List<CategoryTree> trees = new ArrayList<>();
        CategoryTree node;
        for (ShopCategory category : categorys) {
            if (category.getParentId().equals(category.getCategoryId())) {
                continue;
            }
            node = new CategoryTree();
            node.setId(category.getCategoryId());
            node.setParentId(category.getParentId());
            node.setCategory_name(category.getCategoryName());
            node.setInstructions(category.getInstructions());
            node.setCreate_time(category.getCreateTime());
            node.setStatus(category.getStatus());
            trees.add(node);
        }
        return TreeUtil.bulid(trees, 0);
    }

    private void addInputBoxValue(@RequestBody ShopProductVo shopProductVo, ShopProductValue shopProductValue) {
        for (Integer key : shopProductVo.getAttrMap2().keySet()) {
            String value = shopProductVo.getAttrMap2().get(key);
            List<ShopAttrValue> list = shopAttrValueMapper.selectDefaultAttrValue(key, value);
            if (list == null || list.size() == 0) {
                ShopAttrValue shopAttrValue = new ShopAttrValue();
                shopAttrValue.setValue(value);
                shopAttrValue.setCreateTime(new Date());
                shopAttrValue.setAddLocation(0);
                shopAttrValue.setStatus(1);
                shopAttrValue.setAttrId(key);
                shopAttrValueMapper.insert(shopAttrValue);
                Integer shopAttrValueId = shopAttrValueMapper.selectDefaultAttrValue(key, value).get(0).getAttrValueId();
                shopProductValue.setAttrValueId(shopAttrValueId);
            } else {
                Integer shopAttrValueId = list.get(list.size() - 1).getAttrValueId();
                shopProductValue.setAttrValueId(shopAttrValueId);
            }
            shopProductValueMapper.insert(shopProductValue);
        }
    }

    private List<shopAttrVo> getEMPTAttr(Integer categoryId) {
        List<ShopCategory> list3 = getParent(categoryId);
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(categoryId);
        list3.add(shopCategory);
        List<shopAttrVo> list1 = new ArrayList<>();
        for (ShopCategory shopCategory1 : list3) {
            List<ShopAttr> list = shopAttrMapper.selectByCategory(shopCategory1.getCategoryId());
            if (list == null || list.size() == 0) {
                continue;
            }

            for (ShopAttr shopAttr : list) {
                List<ShopAttrValue> list2 = shopAttrValueMapper.selectByAttrId(shopAttr.getAttrId());
                shopAttrVo shopAttrVo = new shopAttrVo();
                BeanUtils.copyProperties(shopAttr, shopAttrVo);
                shopAttrVo.setShopAttrValueList(list2);
                System.out.println(shopAttrVo.toString());
                list1.add(shopAttrVo);
            }
        }
        list1.sort(Comparator.comparingInt(shopAttrVo::getOrderNum));
        return list1;
    }

    private List<ShopCategory> getParent(Integer categoryId) {
        List<ShopCategory> list = new ArrayList<>();
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(categoryId);
        ShopCategory shopCategory1 = shopCategoryMapper.selectCategory(shopCategory.getParentId());
        if (shopCategory1 == null) {
            return list;
        }
        list.add(shopCategory1);
        if (shopCategory1.getParentId() != 0) {
            List<ShopCategory> list1 = this.getParent(shopCategory1.getCategoryId());
            list.addAll(list1);
        }
        return list;
    }

    public ServerResponse getProductDetail(Integer product_id, Integer dept_id) {
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        if (shop_id == null) {
            return ServerResponse.createByErrorMessage("商家不存在");
        }
        ShopProduct shopProduct = shopProductMapper.selectByPrimaryKey(product_id);
        Map<String, Object> map = new HashMap<>();
        //产品id
        map.put("product_id", shopProduct.getProductId());
        //所属产品类目
        List<ShopCategory> list = shopCategoryMapper.selectByShopId(shop_id);
        for (ShopCategory shopCategory : list) {
            if (shopCategory.getInstructions() == null) {
                shopCategory.setInstructions("");
            }
        }
        List<CategoryTree> list2 = getCategoryTree(list);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("categoryTree", list2);
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(shopProduct.getCategoryId());
        map1.put("selected", shopCategory.getCategoryName());
        map.put("category", map1);
        //SKU名称
        map.put("title", shopProduct.getTitle());
        //副标题
        map.put("subTitle", shopProduct.getSubTitle());
        //产品型号
        map.put("model", shopProduct.getModel());
        //类目属性
        List<ShopAttrValue> shopProductValueList = shopAttrValueMapper.selectByProductId(product_id);
        List<ShopCategory> list3 = getParent(shopProduct.getCategoryId());
        list3.add(shopCategory);
        Map<String, Map<String, Object>> map3 = new HashMap<>();
        for (ShopCategory shopCategory1 : list3) {
            List<ShopAttr> attrList = shopAttrMapper.selectByCategory(shopCategory1.getCategoryId());
            if (attrList == null || attrList.size() == 0) {
                continue;
            }
            for (ShopAttr shopAttr : attrList) {
                List<ShopAttrValue> attrValueList = shopAttrValueMapper.selectByAttrId(shopAttr.getAttrId());
                shopAttrVo shopAttrVo = new shopAttrVo();
                BeanUtils.copyProperties(shopAttr, shopAttrVo);
                shopAttrVo.setShopAttrValueList(attrValueList);
                Map<String, Object> map2 = new HashMap<>();
                map2.put("orderNum", shopAttr.getOrderNum());
                map2.put("options", attrValueList);
                map2.put("selected", "");
                if (attrValueList.size() == 0) {
                    for (ShopAttrValue shopAttrValue : shopProductValueList) {
                        if (shopAttr.getAttrId().equals(shopAttrValue.getAttrId())) {
                            map2.put("selected", shopAttrValue);
                        }
                    }
                } else {
                    //寻找选中的选项
                    for (ShopAttrValue shopAttrValue : shopProductValueList) {
                        for (ShopAttrValue shopAttrValue1 : attrValueList) {
                            if (shopAttrValue.getAttrValueId().equals(shopAttrValue1.getAttrValueId())) {
                                map2.put("selected", shopAttrValue);
                            }
                        }
                    }
                }
                map3.put(shopAttrVo.getAttrName(), map2);
            }
        }
        map.put("attr", map3);
        //纤度
        map.put("fineness", shopProduct.getFineness());
        //孔数
        map.put("poreNumber", shopProduct.getPoreNumber());
        //等级
        List<ShopProductGrade> shopProductGradeList = shopProductGradeMapper.selectByProductId(product_id);
        map.put("grade", shopProductGradeList);
        //状态
        map.put("status", shopProduct.getStatus());
        //是否是新品
        map.put("isNew", shopProduct.getIsNew());
        //仓储地
        map.put("wareHouse", shopProduct.getWareHouse());
        //备注
        map.put("remark", shopProduct.getRemark());
        return ServerResponse.createBySuccess(map);
    }

    public ServerResponse getProductList(Integer dept_id, String keyword, int pageNum, int pageSize) {
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        if (shop_id == null) {
            return ServerResponse.createByErrorMessage("商家不存在");
        }
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = shopProductMapper.selectByCountKeyword(keyword, shop_id);
        List<ShopProductListVo> listVos = shopProductMapper.selectByKeyword(keyword, shop_id, pageStart, pageSize);
        List<ShopProductListVo> listVos1 = new ArrayList<>();
        //拼接产品名称
        for (ShopProductListVo shopProductListVo : listVos) {
            List<ShopAttrValue> list = shopAttrValueMapper.selectByProductId(shopProductListVo.getProductId());
            StringBuilder productName = new StringBuilder();
            for (ShopAttrValue shopAttrValue : list) {
                productName.append(shopAttrValue.getValue()).append(",");
            }
            productName = new StringBuilder(productName.substring(0, productName.length() - 1));
            ShopProductListVo shopProductListVo1 = new ShopProductListVo();
            BeanUtils.copyProperties(shopProductListVo, shopProductListVo1);
            shopProductListVo1.setProductName(productName.toString());
            listVos1.add(shopProductListVo1);
        }

        PageVo<ShopProductListVo> pageVo = new PageVo<>(pageNum, pageSize, total, listVos1);
        return ServerResponse.createBySuccess(pageVo);
    }


    public ServerResponse<Object> getEPMIAttr(Integer categoryId) {
        List<shopAttrVo> list = getEMPTAttr(categoryId);
        return ServerResponse.createBySuccess(JSON.toJSON(list));
    }

    public ServerResponse getGradeTemplate(Integer dept_id) {
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        if (shop_id == null) {
            return ServerResponse.createByErrorMessage("商家不存在");
        }
        List<ShopGrade> list = shopGradeMapper.selectByShopId(shop_id);
        return ServerResponse.createBySuccess(list);
    }


    public ServerResponse<String> deleteGradeTemplate(Integer id) {
        ShopGrade shopGrade = shopGradeMapper.selectByPrimaryKey(id);
        shopGrade.setStatus(0);
        shopGradeMapper.updateByPrimaryKey(shopGrade);
        return ServerResponse.createBySuccess("删除成功");
    }

    public ServerResponse addGradeTemplate(Integer dept_id, String grade, String name) {
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        if (shop_id == null) {
            return ServerResponse.createByErrorMessage("商家不存在");
        }
        ShopGrade shopGrade = new ShopGrade();
        shopGrade.setStatus(1);
        shopGrade.setCanEdit(1);
        shopGrade.setName(name);
        shopGrade.setGrade(grade);
        shopGrade.setCreateTime(new Date());
        shopGrade.setShopId(shop_id);
        shopGradeMapper.insert(shopGrade);
        return ServerResponse.createBySuccess("创建成功");
    }


    public ServerResponse<String> updateGradeTemplate(ShopGrade shopGrade) {
        shopGrade.setUpdateDate(new Date());
        shopGradeMapper.updateByPrimaryKey(shopGrade);
        return ServerResponse.createBySuccess("更新完成");
    }

    public ServerResponse addShopProduct(ShopProductVo shopProductVo) {
        try {
            ShopProduct shopProduct = new ShopProduct();
            BeanUtils.copyProperties(shopProductVo, shopProduct);
            shopProduct.setCreateTime(new Date());
            shopProductMapper.insert(shopProduct);
            Integer product_id = shopProductMapper.selectByCategoryIdAndTitle(shopProductVo.getCategoryId(), shopProductVo.getTitle());
            ShopProductValue shopProductValue = new ShopProductValue();
            shopProductValue.setProductId(product_id);
            shopProductValue.setCreateTime(new Date());
            shopProductValue.setStatus(1);
            //添加单选框属性
            for (Integer key : shopProductVo.getAttrMap().keySet()) {
                shopProductValue.setAttrValueId(shopProductVo.getAttrMap().get(key));
                shopProductValueMapper.insert(shopProductValue);
            }
            //添加输入框属性
            addInputBoxValue(shopProductVo, shopProductValue);
            //添加等级
            ShopProductGrade shopProductGrade = new ShopProductGrade();
            shopProductGrade.setCreateTime(new Date());
            shopProductGrade.setProductId(product_id);
            shopProductGrade.setStatus(1);
            addGrade(shopProductVo, shopProductGrade);
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("参数有异常，请联系后端人员");
        }
        return ServerResponse.createBySuccess("添加成功");
    }

    public ServerResponse getCategoryList(Integer dept_id) {
        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
        if (shop_id == null) {
            return ServerResponse.createByErrorMessage("商家不存在");
        }
        //4.3修改可以看到禁用的值
        List<ShopCategory> list = shopCategoryMapper.selectByShopId2(shop_id);
        for (ShopCategory shopCategory : list) {
            if (shopCategory.getInstructions() == null) {
                shopCategory.setInstructions("");
            }
        }
        List<CategoryTree> list2 = getCategoryTree(list);
        Map<String, Object> map = new HashMap<>();
        map.put("shop_id", shop_id);
        map.put("categoryList", list2);
        return ServerResponse.createBySuccess(map);
    }


    public ServerResponse<List<ShopAttr>> getAttrList(Integer categoryId) {
        List<ShopAttr> list1 = shopAttrMapper.selectByCategory(categoryId);
        for (ShopAttr shopAttr : list1) {
            if (shopAttr.getInstructions() == null) {
                shopAttr.setInstructions("");
            }
        }
        return ServerResponse.createBySuccess(list1);
    }


    public ServerResponse<Map<String, List<ShopAttr>>> getAttrWithParentList(Integer categoryId) {
        List<ShopAttr> list1 = shopAttrMapper.selectByCategory(categoryId);
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(categoryId);
        if (shopCategory == null) {
            return ServerResponse.createByErrorMessage("该目录已被禁用");
        }
        List<ShopCategory> list = getParent(categoryId);
        Map<String, List<ShopAttr>> map = new HashMap<>();
        map.put(shopCategory.getCategoryName(), list1);
        for (ShopCategory shopCategory1 : list) {
            List<ShopAttr> list2 = shopAttrMapper.selectByCategory(shopCategory1.getCategoryId());
            for (ShopAttr shopAttr : list2) {
                if (shopAttr.getInstructions() == null) {
                    shopAttr.setInstructions("");
                }
            }
            map.put(shopCategory1.getCategoryName(), list2);
        }
        return ServerResponse.createBySuccess(map);
    }

    public ServerResponse<List<ShopAttrValue>> getAttrValueList(Integer attrId) {
        List<ShopAttrValue> list = shopAttrValueMapper.selectByAttrId(attrId);
        return ServerResponse.createBySuccess(list);
    }


    public ServerResponse addCategory(ShopCategory shopCategory) {
        MallShop mallShop = mallShopMapper.selectByPrimaryKey(shopCategory.getShopId());
        if (mallShop == null) {
            return ServerResponse.createByErrorMessage("创建失败，shop_id错误");
        }
        //4.4 要验证已禁用目录
        List<ShopCategory> list = shopCategoryMapper.selectRepeatDataWithDisable(shopCategory.getCategoryName(), shopCategory.getShopId(), shopCategory.getParentId());
        if (list.size() > 0) {
            return ServerResponse.createByErrorMessage("创建失败，已存在同名目录");
        }
        shopCategory.setCreateTime(new Date());
        shopCategory.setUpdateTime(new Date());
        shopCategory.setStatus(1);
        shopCategoryMapper.insert(shopCategory);
        return ServerResponse.createBySuccess("创建成功");
    }


    public ServerResponse updateCategory(ShopCategory shopCategory) {
        MallShop mallShop = mallShopMapper.selectByPrimaryKey(shopCategory.getShopId());
        if (mallShop == null) {
            return ServerResponse.createByErrorMessage("创建失败，shop_id错误");
        }
        List<ShopCategory> list = shopCategoryMapper.selectExceptSelfRepeatData(shopCategory.getCategoryName(), shopCategory.getShopId(), shopCategory.getCategoryId(), shopCategory.getParentId());
        if (list.size() > 0) {
            return ServerResponse.createByErrorMessage("创建失败，已存在同名目录");
        }
        shopCategory.setUpdateTime(new Date());
        shopCategory.setStatus(1);
        shopCategoryMapper.updateByPrimaryKey(shopCategory);
        return ServerResponse.createBySuccess("创建成功");
    }

    public ServerResponse deleteCategory(Integer categoryId) {
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(categoryId);
        if (shopCategory == null) {
            return ServerResponse.createByErrorMessage("目录不存在");
        }
        shopCategory.setStatus(0);
        shopCategory.setUpdateTime(new Date());
        shopCategoryMapper.updateByPrimaryKey(shopCategory);
        //更新子节点的状态
        List<ShopCategory> list = getChildren(categoryId);
        for (ShopCategory shopCategory1 : list) {
            shopCategory1.setStatus(0);
            shopCategoryMapper.updateByPrimaryKey(shopCategory1);
        }
        //更新含有改目录的旧产品为禁用  4.3不需要修改  查询产品的时候需要查看categoryId是否有效
//        shopProductMapper.updateCategoryStatus(categoryId);
//        for (ShopCategory shopCategory1 : list){
//            shopProductMapper.updateCategoryStatus(shopCategory1.getCategoryId());
//        }
        return ServerResponse.createBySuccess("禁用成功");
    }

    //获得该目录下的所有子目录
    private List<ShopCategory> getChildren(Integer categoryId) {
        List<ShopCategory> list = shopCategoryMapper.selectByParentId(categoryId);
        List<ShopCategory> list2 = new ArrayList<>(list);
        Integer count;
        for (ShopCategory shopCategory : list) {
            count = shopCategoryMapper.checkHaveChild(shopCategory.getCategoryId());
            if (count != 0) {
                List<ShopCategory> list1 = this.getChildren(shopCategory.getCategoryId());
                list2.addAll(list1);
            }
        }
        return list2;
    }

    public ServerResponse deleteAttrValue(Integer attrValueId) {
        ShopAttrValue shopAttrValue = shopAttrValueMapper.selectAttrValue(attrValueId);
        if (shopAttrValue == null) {
            return ServerResponse.createByErrorMessage("属性值不存在");
        }
        shopAttrValue.setStatus(0);
        shopAttrValue.setUpdateTime(new Date());
        shopAttrValueMapper.updateByPrimaryKey(shopAttrValue);
        //更新旧产品
        shopProductValueMapper.updateAttrValueStatus(attrValueId);
        return ServerResponse.createBySuccess("禁用成功");
    }

    public ServerResponse updateAttrValue(ShopAttrValue shopAttrValue) {
        ShopAttr shopAttr = shopAttrMapper.selectAttr(shopAttrValue.getAttrId());
        if (shopAttr == null) {
            return ServerResponse.createByErrorMessage("属性不存在");
        }
        List<ShopAttrValue> list = shopAttrValueMapper.selectExceptSelfRepeatData(shopAttrValue.getAttrId(), shopAttrValue.getValue(), shopAttrValue.getAttrValueId());
        if (list.size() != 0) {
            return ServerResponse.createByErrorMessage("创建失败，已存在同名属性值");
        }
        shopAttrValue.setAddLocation(1);
        shopAttrValue.setStatus(1);
        shopAttrValue.setUpdateTime(new Date());
        shopAttrValueMapper.updateByPrimaryKey(shopAttrValue);
        return ServerResponse.createBySuccess("创建成功");
    }

    public ServerResponse addAttrValue(ShopAttrValue shopAttrValue) {
        ShopAttr shopAttr = shopAttrMapper.selectAttr(shopAttrValue.getAttrId());
        if (shopAttr == null) {
            return ServerResponse.createByErrorMessage("属性不存在");
        }
        List<ShopAttrValue> list = shopAttrValueMapper.selectRepeatData(shopAttrValue.getAttrId(), shopAttrValue.getValue());
        if (list.size() != 0) {
            return ServerResponse.createByErrorMessage("创建失败，已存在同名属性值");
        }
        shopAttrValue.setAddLocation(1);
        shopAttrValue.setStatus(1);
        shopAttrValue.setCreateTime(new Date());
        shopAttrValue.setUpdateTime(new Date());
        shopAttrValueMapper.insert(shopAttrValue);
        return ServerResponse.createBySuccess("创建成功");
    }

    public ServerResponse deleteAttr(Integer attrId) {
        ShopAttr shopAttr = shopAttrMapper.selectAttr(attrId);
        if (shopAttr == null) {
            return ServerResponse.createByErrorMessage("属性不存在");
        }
        shopAttr.setStatus(0);
        shopAttr.setUpdateTime(new Date());
        shopAttrMapper.updateByPrimaryKey(shopAttr);
        //更新旧产品
        shopProductValueMapper.updateAttrStatus(attrId);
        return ServerResponse.createBySuccess("禁用成功");
    }

    public ServerResponse updateAttr(ShopAttr shopAttr) {
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(shopAttr.getCategoryId());
        if (shopCategory == null) {
            return ServerResponse.createByErrorMessage("目录不存在");
        }
        List<ShopAttr> list = shopAttrMapper.selectExceptSelfRepeatData(shopAttr.getAttrName(), shopAttr.getCategoryId(), shopAttr.getAttrId());
        if (list.size() != 0) {
            return ServerResponse.createByErrorMessage("创建失败，已存在同名属性");
        }
        shopAttr.setUpdateTime(new Date());
        shopAttr.setStatus(1);
        shopAttrMapper.updateByPrimaryKey(shopAttr);
        return ServerResponse.createBySuccess("创建成功");
    }


    public ServerResponse addAttr(ShopAttr shopAttr) {
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(shopAttr.getCategoryId());
        if (shopCategory == null) {
            return ServerResponse.createByErrorMessage("目录不存在");
        }
        //校验父级子级是否有重复
        List<ShopCategory> list2 = getParent(shopAttr.getCategoryId());
        List<ShopCategory> list3 = getChildren(shopAttr.getCategoryId());
        list2.add(shopCategory);
        list2.addAll(list3);
        List<ShopAttr> list = new ArrayList<>();
        for (ShopCategory shopCategory1 : list2) {
            List<ShopAttr> list4 = shopAttrMapper.selectRepeatData(shopAttr.getAttrName(), shopCategory1.getCategoryId());
            list.addAll(list4);
        }
        if (list.size() != 0) {
            return ServerResponse.createByErrorMessage("创建失败，已存在同名属性");
        }
        shopAttr.setCanDelete(1);
        shopAttr.setCreateTime(new Date());
        shopAttr.setUpdateTime(new Date());
        shopAttr.setStatus(1);
        shopAttrMapper.insert(shopAttr);
        //更新旧产品
        Integer attr_id = shopAttrMapper.selectRepeatData(shopAttr.getAttrName(), shopAttr.getCategoryId()).get(0).getAttrId();
        ShopAttrValue shopAttrValue = new ShopAttrValue();
        shopAttrValue.setStatus(1);
        shopAttrValue.setAttrId(attr_id);
        shopAttrValue.setAddLocation(0);
        shopAttrValue.setCreateTime(new Date());
        shopAttrValue.setValue(shopAttr.getDefaultValue());
        shopAttrValueMapper.insert(shopAttrValue);
        Integer attr_value_id = shopAttrValueMapper.selectDefaultAttrValue(attr_id, shopAttr.getDefaultValue()).get(0).getAttrValueId();
        List<ShopProduct> list1 = shopProductMapper.selectByCategoryId(shopAttr.getCategoryId());
        ShopProductValue shopProductValue = new ShopProductValue();
        for (ShopProduct shopProduct : list1) {
            shopProductValue.setAttrValueId(attr_value_id);
            shopProductValue.setCreateTime(new Date());
            shopProductValue.setProductId(shopProduct.getProductId());
            shopProductValue.setStatus(1);
            shopProductValueMapper.insert(shopProductValue);
        }
        return ServerResponse.createBySuccess("创建成功");
    }

    public ServerResponse restartCategory(Integer categoryId) {
        ShopCategory shopCategory = shopCategoryMapper.selectByPrimaryKey(categoryId);
        if (shopCategory == null) {
            return ServerResponse.createByErrorMessage("目录不存在");
        }
        shopCategory.setStatus(1);
        shopCategory.setUpdateTime(new Date());
        shopCategoryMapper.updateByPrimaryKey(shopCategory);
        //更新子节点的状态
        List<ShopCategory> list = getChildren(categoryId);
        for (ShopCategory shopCategory1 : list) {
            shopCategory1.setStatus(0);
            shopCategoryMapper.updateByPrimaryKey(shopCategory1);
        }
        return ServerResponse.createBySuccess("启用成功");
    }

    public ServerResponse<ShopCategory> getCategoryDetail(Integer categoryId) {
        ShopCategory shopCategory = shopCategoryMapper.selectByPrimaryKey(categoryId);
        if (shopCategory.getInstructions() == null) {
            shopCategory.setInstructions("");
        }
        return ServerResponse.createBySuccess(shopCategory);
    }

    public ServerResponse<PageVo<ShopShareVo>> getSharesDetail(String keyword, Integer userId, int pageNum, int pageSize, String type) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = shopSharesMapper.selectCountByKeyword(keyword, type);
        List<ShopShareVo> list = shopSharesMapper.selectByKeyword(keyword, pageStart, pageSize, type);
        List<Integer> list1 = shopUserSharesMapper.selectByUserId(userId);
        //设置状态1已选择 0未选择
        for (ShopShareVo shopShares : list) {
            if (list1.contains(shopShares.getId())) {
                shopShares.setStatus(1);
            } else {
                shopShares.setStatus(0);
            }
        }
        PageVo<ShopShareVo> pageVo = new PageVo<>(pageNum, pageSize, total, list);
        return ServerResponse.createBySuccess(pageVo);
    }


    public ServerResponse<List<MallShop>> getShopList() {
        List<MallShop> list = mallShopMapper.selectAll();
        return ServerResponse.createBySuccess(list);
    }

    public ServerResponse<List<shopAttrVo>> getFilter(Integer categoryId) {
        List<ShopCategory> list3 = getParent(categoryId);
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(categoryId);
        list3.add(shopCategory);
        List<shopAttrVo> list1 = new ArrayList<>();
        for (ShopCategory shopCategory1 : list3) {
            List<ShopAttr> list = shopAttrMapper.selectByCategory(shopCategory1.getCategoryId());
            if (list == null || list.size() == 0) {
                continue;
            }

            for (ShopAttr shopAttr : list) {
                List<ShopAttrValue> list2 = shopAttrValueMapper.selectByAttrId2(shopAttr.getAttrId());
                shopAttrVo shopAttrVo = new shopAttrVo();
                BeanUtils.copyProperties(shopAttr, shopAttrVo);
                shopAttrVo.setShopAttrValueList(list2);
                System.out.println(shopAttrVo.toString());
                list1.add(shopAttrVo);
            }
        }
        list1.sort(Comparator.comparingInt(shopAttrVo::getOrderNum));
        return ServerResponse.createBySuccess(list1);
    }

    public ServerResponse filterProduct(Integer shopId, Integer categoryId, Map<String, String> map, Integer pageSize, Integer pageNum) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        List<Integer> list = new ArrayList<>();
        ShopAttrValue shopAttrValue;
        for (String key : map.keySet()) {
            shopAttrValue = shopAttrValueMapper.selectByAttrIdAndValue(Integer.parseInt(key), map.get(key));
            list.add(shopAttrValue.getAttrValueId());
        }
        total = shopProductMapper.selectCountByFilterItem(categoryId, list, list.size());
        List<ShopProduct> list1 = shopProductMapper.selectByFilterItem(categoryId, list, list.size(), pageStart, pageSize);
        List<FilterProductVo> list2 = new ArrayList<>();
        for (ShopProduct shopProduct : list1) {
            MallShop mallShop = mallShopMapper.selectByPrimaryKey(shopId);
            FilterProductVo filterProductVo = new FilterProductVo();
            filterProductVo.setShopName(mallShop.getShopName());
            filterProductVo.setProductId(shopProduct.getProductId());
            List<ShopAttrValue> list3 = shopAttrValueMapper.selectByProductId(shopProduct.getProductId());
            StringBuilder productName = new StringBuilder();
            for (int i = 0; i < list3.size(); i++) {
                productName.append(list3.get(i).getValue()).append("-");
                if (i == 4) {
                    break;
                }
            }
            productName = new StringBuilder(productName.substring(0, productName.length() - 1));
            filterProductVo.setProductName(productName.toString());
            filterProductVo.setSpecifications(shopProduct.getFineness() + "/" + shopProduct.getPoreNumber());
            filterProductVo.setModel(shopProduct.getModel());
            filterProductVo.setImages(shopProduct.getImages());
            filterProductVo.setIsNew(shopProduct.getIsNew());
            list2.add(filterProductVo);
        }
        if (total == null) {
            total = 0;
        }
        PageVo pageVo = new PageVo<>(pageNum, pageSize, total, list2);
        return ServerResponse.createBySuccess(pageVo);
    }

    public ServerResponse<Map<String, Object>> getFilterProductDetail(Integer productId) {
        ShopProduct shopProduct = shopProductMapper.selectByPrimaryKey(productId);
        Map<String, Object> map = new HashMap<>();
        map.put("title", shopProduct.getTitle());
        map.put("subTitle", shopProduct.getSubTitle());
        map.put("model", shopProduct.getModel());
        map.put("productId", productId);
        List<ShopProductGrade> list = shopProductGradeMapper.selectByProductId(productId);
        map.put("grade", list);
        List<ShopAttrValue> list1 = shopAttrValueMapper.selectByProductId(productId);
        List<ShopAttrValueVo> list3 = new ArrayList<>();
        for (ShopAttrValue shopAttrValue : list1) {
            ShopAttr shopAttr = shopAttrMapper.selectAttr(shopAttrValue.getAttrId());
            ShopAttrValueVo shopAttrValueVo = new ShopAttrValueVo();
            BeanUtils.copyProperties(shopAttrValue, shopAttrValueVo);
            shopAttrValueVo.setAttrName(shopAttr.getAttrName());
            list3.add(shopAttrValueVo);
        }
        map.put("AttrValue", list3);
        map.put("images", shopProduct.getImages());
        map.put("detail", shopProduct.getDetail());
        return ServerResponse.createBySuccess(map);
    }

    public ServerResponse getSecondCategory(Integer categoryId) {
        ShopCategory shopCategory = shopCategoryMapper.selectCategory(categoryId);
        List<ShopCategory> list = getChildren(categoryId);
        list.add(shopCategory);
        List<CategoryTree> list1 = getCategoryTree(list);
        return ServerResponse.createBySuccess(list1);
    }
}
